package com.itheima.controller;


import com.itheima.commo.PageResult;
import com.itheima.commo.R;
import com.itheima.dto.DishDto;
import com.itheima.entity.Dish;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;


    /**
     * 新增菜品
     * DishDto：包含菜品+菜品口味数据（前后端保持一致）
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }


    /**
     * 菜品分页查询
     *
     */
    @GetMapping("/page")
    public R<PageResult> findPage(@RequestParam(value = "page", defaultValue = "1") Long page,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize, String name) {
        PageResult<DishDto> pageResult = dishService.findPage(page, pageSize,name);
        return R.success(pageResult);
    }

    /**
     * 根据菜品主键id查询菜品数据（菜品表+菜品口味表数据）
     */
    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable("id") Long id){
        return R.success(dishService.findById(id));
    }

    /**
     * 菜品修改
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.update(dishDto);
        return R.success("修改菜品信息成功了");
    }


    /**
     * 通过菜品分类id 查询菜品列表数据
     */
   /* @GetMapping("/list")
    public R<List<Dish>> list(Dish dish){
        List<Dish> dishList =  dishService.list(dish);
        return R.success(dishList);
    }*/

    /**
     * 根据菜品分类id查询菜品列表数据
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        List<DishDto> dishDtoList =  dishService.list(dish);
        return R.success(dishDtoList);
    }


    /**
     * 删除套餐 菜品表关联数据
     */
    @DeleteMapping
    public R<Dish> delete(@RequestParam List<Long> ids){
        dishService.delete(ids);
        return R.success("删除id成功");
    }

    /**
     * 菜品批量启售 停售
     */
    @PostMapping("/status/{status}")
    public R<Dish> statusA(@PathVariable Integer status,@RequestParam List<Long> ids){
        return dishService.statusA(status, ids);
    }
}
